Buffself = class('Buffself')

function Buffself:initialize(infopunk)
	self.name = "buffself"
	self.infopunk = setmetatable({}, {__index = infopunk})
	self.skapi = setmetatable({}, {__index = infopunk.skapi })
	self.buffing = false
	self.buff_finished = true
	self.buff_queue = {}

	print("buffself initialized")

end

function Buffself:buff_a_list(target, list, buff_target)
	print("in buff a list")
	
	print(list[1])
	print(self.skapi:SpellidFromSz(list[1]))
	
	
	print("on action declared")
	print("list #: ".. #list)

	local new_list = {}
	for i = 1, #list do
		print("list iter ".. i)
		new_list[i] = {}
		if buff_target then
			print("adding " .. list[i] .. " to new_list with target: " .. buff_target)
			new_list[i].name = list[i]
			new_list[i].target = buff_target
		else
			print("adding " .. list[i] .. " to new list")
			new_list[i].name = list[i]
			new_list[i].target = nil
		end
	end
	
	print("new list size: " .. #new_list)
	

	--self.skapi:CastSpell(skapi:SpellidFromSz(list[1]), buff_target)
	self:add_list_to_queue(new_list)
	self.buffing = true

end

function Buffself:add_list_to_queue(list)

	for i = 1, #list do
		self.buff_queue[#self.buff_queue+1] = list[i]
		print("added " .. list[i].name .." to queue ")
	end

end

function Buffself:update(dt)
	
	if self.buffing then
		if self.buff_finished then
			local q = self.buff_queue[1]
			if q then
				print('casting .. ' .. q.name)								
				local id = self.skapi:SpellidFromSz(q.name)
				print('id : ' .. id)
				if self.skapi:FSpellidInSpellbook(id) then
					print(self.skapi.szName .. ' has spell')
					self.skapi:CastSpell(id, q.target)	
					self.buff_finished = false
				else
					print(self.skapi.szName .. ' does not have spell')
				end
			else
				self.buffing = false
				self.buff_queue = {}
			end			
		end
	end
end

function Buffself:cmd(cmd, player)
	print("buffself got cmd " .. cmd)
	local a_cmd = cmd:sub(1, 1)
	print("a_cmd = " .. a_cmd)
	local actual_command

	if a_cmd == "a" then
		local cmd_s = cmd:sub(2,5)
		print("second sub:" .. cmd_s)
		if cmd_s == "rmor" then
			actual_command = "armor"
			self:buff_armor(player)
		end
	elseif a_cmd == "i" then
		if cmd:sub(2,4) == "tem" then
			actual_command = "item"
		end
	elseif a_cmd == "l" then
		if cmd:sub(2,4) == "ife" then
			actual_command ="life"
		end
	elseif a_cmd == "s" then
		if cmd:sub(2,6) == "hield" then
			actual_command ="shield"
		end
	end

	print("actual command: " .. actual_command)	


end

function Buffself:buff_armor(target)
	print("in buff armor")
	print(self.name)
	print("target name: " .. target.name)
	self:buff_a_list(target, target.buff_armor_list)

end